package com.priox.sigar.application.job.mappers;

import com.priox.sigar.client.dto.AcreedorDTO;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindException;

/**
 * User: Matrix
 * Date: 10/02/13
 */
@Component
public class AcreedorFieldSetMapper implements FieldSetMapper<AcreedorDTO> {
    private static final Logger LOGGER = LoggerFactory.getLogger(AcreedorFieldSetMapper.class);
    /*
     * (non-Javadoc)
     *
     * @see
     * org.springframework.batch.item.file.mapping.FieldSetMapper#mapFieldSet
     * (org.springframework.batch.item.file.transform.FieldSet)
     */

    public AcreedorDTO mapFieldSet(FieldSet fieldSet) throws BindException {
        AcreedorDTO result = new AcreedorDTO();
        try {
            if(NumberUtils.isNumber(fieldSet.readString(0))
                    && StringUtils.isNotBlank(fieldSet.readString(1))
                    && fieldSet.readString(0).length()==3){
                result.setSociedadId((fieldSet.readString(0)));
                result.setAcreedorId((fieldSet.readString(1)));
                result.setRazonSocial(fieldSet.readString(2));
            }
        } catch (Exception ex) {
            LOGGER.error("Error al leer acreedor "+fieldSet);
            StackTraceElement[] err=ex.getStackTrace();
            for(Object ob:err) {
                LOGGER.error("error :"+ob.toString());   }
            LOGGER.error("ERROR2"+ex)  ;
        }
        return result;
    }
}

